//
//  JMShare.m
//  JMLibrary
//
//  Created by zhengxianda on 2018/7/20.
//

#import "JMShare.h"

#import "JMSina.h"
#import "JMWeChat.h"
#import "JMTencent.h"

@interface JMShare ()

@property(nonatomic, strong) JMSina * sina;
@property(nonatomic, strong) JMWeChat * wechat;
@property(nonatomic, strong) JMTencent * tencent;

@end

@implementation JMShare

+ (instancetype)shared{
    static JMShare *shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shared = [[self alloc] init];
        shared.sina = [JMSina new];
        shared.wechat = [JMWeChat new];
        shared.tencent = [JMTencent new];
    });
    return shared;
}

+ (void)registerPlatform:(JMSharePlatform)platform withAppId:(NSString *)appId{
    JMShare *share = [JMShare shared];
    switch (platform) {
        case JMSharePlatformSina:
            [share.sina registerApp:appId];
        case JMSharePlatformWeChat:
            [share.wechat registerApp:appId];
        case JMSharePlatformTencent:
            [share.tencent registerApp:appId];
            break;
    }
}

+ (BOOL)handleOpenURL:(NSURL *)url{
    JMShare *share = [JMShare shared];
    return
    [share.sina handleOpenURL:url] ||
    [share.wechat handleOpenURL:url] ||
    [share.tencent handleOpenURL:url];
}

+ (void)shareToChannel:(JMShareChannel)channel
         messageObject:(JMShareMessageObject *)messageObject
               handler:(JMShareRequestHandler)handler{
    JMShare *share = [JMShare shared];
    switch (channel) {
        case JMShareChannelSinaWeibo:
            [share.sina shareToChannel:channel messageObject:messageObject handler:handler];
            break;
        case JMShareChannelWeChatMessage:
        case JMShareChannelWeChatTimeline:
            [share.wechat shareToChannel:channel messageObject:messageObject handler:handler];
            break;
        case JMShareChannelTencentQQ:
        case JMShareChannelTencentQQZone:
            [share.tencent shareToChannel:channel messageObject:messageObject handler:handler];
            break;
            
        default:
            break;
    }
}

@end
